<?php

namespace app\api\controller;

class Music
{
    public $pagesize = 10;

    /**
     * 音乐大菜单类型
     * @return \think\response\Json
     */
    public function typeLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }
        $lists = [
            array(
                "attr_key" => "0",
                "attr_value" => "自然音",
                "display_name" => "自然音"),
            array(
                "attr_key" => "15",
                "attr_value" => "睡前",
                "display_name" => "睡前"),
            array(
                "attr_key" => "14",
                "attr_value" => "灵魂",
                "display_name" => "灵魂"),
            array(
                "attr_key" => "127",
                "attr_value" => "纯音乐",
                "display_name" => "纯音乐"),

            array(
                "attr_key" => "127",
                "attr_value" => "催眠",
                "display_name" => "催眠"),
            array(
                "attr_key" => "127",
                "attr_value" => "治愈系",
                "display_name" => "治愈系"),
            array(
                "attr_key" => "14",
                "attr_value" => "轻音乐",
                "display_name" => "轻音乐")
        ];
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    /**
     * 获取音乐列表接口
     */
    public function lists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $attr_key = trim(input("request.attr_key"));//
        $pid = trim(input("request.pid"));//
        if(empty($pid)){
            $pid=0;
        }
        if (empty($attr_key)) {
            $attr_key = 0;
        }
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }
        $lists = array();
        switch ($attr_key) {
            case 0:
                $lists = $this->getNaturalMusic($pid);
                break;
        }

        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);

    }

    /**
     * 获取自然音列表
     */
    public function getNaturalMusic($pid=0)
    {
        $pagesize = trim(input("pagesize"));//每页条数
        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $w = array();
        $w['is_delete'] = 0;//未删除
        $w['stats'] = 0;//已上架
        if(!empty($pid)){
            $w['pid']=$pid;
        }
        $model = new \app\admin\model\Music();
        $lists = $model->lists($w, $this->pagesize, "*", "sorts asc");
        $lists = $lists['data'];
        $lists = formateTimeForArr($lists, "ctime");
        return $lists;
    }

    public function play()
    {
        $path = ROOT_PATH . 'public' . DS . "static" . DS . "admin" . DS . "main.mp3";
        $file = fopen($path, "rb");
        Header("Content-type: audio/mp3");
        Header("Accept-Ranges: bytes");
        Header("Access-Control-Allow-Origin: *");
        Header("Access-Control-Expose-Headers: X-Log, X-Reqid");
        Header("Content-Transfer-Encoding: binary");
        //请求范围的度量单位
        Header("Accept-Ranges: bytes");
        //Content-Length是指定包含于请求或响应中数据的字节长度
        Header("Accept-Length: " . filesize($path));
        //用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。
        Header("Content-Disposition: inline; filename=\"FvMe31mp9JwA0Sk-NP3domZ-kw_8\"; filename*=utf-8' 'FvMe31mp9JwA0Sk-NP3domZ-kw_8");
        //读取文件内容并直接输出到浏览器
        echo fread($file, filesize($path));
        fclose($file);
        exit(0);
    }

    /**
     *
     * 获取喜马拉雅签名
     * client_id=b617866c20482d133d5de66fceb37da3&device_id=dasffasddafs12345&nonce=sbby2hyM9M&timestamp=1566285175413&params=%7B%22client_id%22%3A%22xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%22%2C%22device_id%22%3A%22dasffasddafs12345%22%2C%22nonce%22%3A%22sbby2hyM9M%22%2C%22timestamp%22%3A1566285175413%2C%22grant_type%22%3A%22js_client_credentials%22%7D
     * http://127.0.0.1:9998/api/music/sign?client_id=40afce04453a811871084cd42cb8ec7f&device_id=dasffasddafs12345&nonce=dasffasddafs12345&timestamp=1569489962855&params={"client_id":"b617866c20482d133d5de66fceb37da3","device_id":"dasffasddafs12345","nonce":"EBHjX2EF","timestamp":1569489962855,"grant_type":"js_client_credentials"}

     */
    public function sign()
    {
//        app_key:40afce04453a811871084cd42cb8ec7f
//        app_secret:f86c334975aab62271cbec4a9ee88fc8
//        $client_id= trim(input("client_id"));//喜马拉雅开放平台应用公钥 app_key

//        $nonce = trim(input("nonce"));//随机字符串
//        $timestamp = trim(input("timestamp"));//毫秒时间蹉
        http://127.0.0.1:9998/api/music/sign?client_id=40afce04453a811871084cd42cb8ec7f&device_id=dasffasddafs12345&nonce=dasffasddafs12345&timestamp=1569489962855&params={"client_id":"40afce04453a811871084cd42cb8ec7f","device_id":"dasffasddafs12345","nonce":"EBHjX2EF","timestamp":1569489962855,"grant_type":"js_client_credentials"}
        $client_id = "40afce04453a811871084cd42cb8ec7f";//喜马拉雅开放平台应用公钥
        $app_secret="f86c334975aab62271cbec4a9ee88fc8";

        $input_client_id= trim(input("client_id"));
        if($client_id!=$input_client_id)
        {
            $return = array(
                "code" => 1,
                "message" => "client_id为空",
                "signature" => ""
            );
            return json($return);
        }
        $device_id = trim(input("device_id"));//设备 ID
        $params = trim(input("params"));//其他参数
        $paramArr = json_decode($params, true);//用于计算签名的全部业务请求参数， JSON.stringify() 后的结果（开发者服务端需要从这里解析出参数，用于计算签名）

        if(trim($paramArr['client_id']) !=$client_id)
        {
            $return = array(
                "code" => 1,
                "message" => "client_id不正确111",
                "signature" => ""
            );
            return json($return);
        }
        if($paramArr['device_id'] !=$device_id)
        {
            $return = array(
                "code" => 1,
                "message" => "device_id不正确",
                "signature" => ""
            );
            return json($return);
        }

        $paramString = createQueryString($paramArr);//生成查询字符串

        $base64String = base64_encode($paramString);//将查询字符串base64加密

        $hashString = hmac_sha1($base64String, $app_secret);//生成签名

        $sign = md5($hashString);
        $return = array(
            "code" => 0,
            "message" => "success",
            "signature" => $sign
        );
        return json($return);

    }
}
